'''
An RPG Modification for Source games using Source Python.

    Copyright (C) 2013 Steven Hartin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

Created on 6 Jan 2013

@author: Steven Hartin
@file: ../sourcerpg/skills/base/skills.py
'''

from sourcerpg.skills.base.errors import SkillLoaderException


class Skills(dict):
    """A dictionary object which should contain all the skills on the server"""
    def load_skill(self, skill_name):
        """Imports the skill and stores it in this dict.

        Assumes the skill is in the ../sourcerpg/skills/ directory, named the
        same as the skill_name

        @param str skill_name The skill to import and store

        """
        skill_import = None
        skill = None
        try:
            # Attempt to import the skill
            skill_import = __import__("sourcerpg.skills.%s" % skill_name,
                fromlist=["sourcerpg", "skills", skill_name])
        except Exception as exception:
            # An error occurred, through a skill loader exception
            raise SkillLoaderException(str(exception), skill_name)
        try:
            skill = skill_import.Skill
        except AttributeError:
            # The main skill class has not been exposed or named correctly
            raise SkillLoaderException(("There is no 'Skill' class in %s" %
                skill_name), skill_name)
        if skill_import is not None and skill is not None:
            try:
                # Lets assume that the class takes 1 constructor params and
                # it constructs the base with a separate skill name
                self[skill_name] = skill(skill_name)
            except TypeError:
                # Okay, then we can assume it's not overridden the base classes
                # constructor, so we'll pass the internal skill name as an
                # extra param.
                self[skill_name] = skill(skill_name, skill_name)

    def unload_skill(self, skill_name):
        """@param str The skill to unload"""
        del self[skill_name]
